package com.lin.controller;

import com.github.pagehelper.PageInfo;
import com.lin.entity.Info;
import com.lin.service.InfoService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 11:36Li更新代码
 * 使用idea注释演示
 * 使用idea提交代码
 */
@Controller
public class InfoController {

    @Resource
    private InfoService infoService;

    @RequestMapping("/selAll")
    public String selAll(@RequestParam(value = "pageNum",required = true,defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",required = false,defaultValue = "4")Integer pageSize, Model model,Integer booktype,String bookname,Integer isborrowed){
        Integer booktype2=null;
        String bookname2=null;
        Integer isborrowed2=null;
        if (booktype!=null&&booktype!=-1){
            booktype2=booktype;
        }
        if(bookname!=null&&!bookname.equals("")){
            bookname2=bookname;
        }
        if (isborrowed!=null&&isborrowed!=-1){
            isborrowed2=isborrowed;
        }
        PageInfo<Info> pageInfo = infoService.selAll(booktype2, bookname2, isborrowed2, pageNum, pageSize);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("booktype",booktype2);
        model.addAttribute("bookname",bookname2);
        model.addAttribute("isborrowed",isborrowed2);
        return "home";
    }


    @RequestMapping("/upd")
    public String upd(Integer bookid,Integer isborrowed){
        Info info = infoService.selectByPrimaryKey(bookid.longValue());
        if (isborrowed==0){
            info.setIsborrowed(1);
        }else {
            info.setIsborrowed(0);
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        info.setLastupdatetime(df.format(new Date()));
        infoService.updateByPrimaryKey(info);
        return "redirect:/selAll";
    }













}
